﻿angular.module('app.disputetracking', [])
    .config(['$routeProvider', function ($routeProvider) {
        $routeProvider
        .when('/disputetracking', {
            templateUrl: 'app/disputetracking/disputeTracking-list.tpl.html',
            controller: 'disputeTrackingCtrl',
            resolve: {
            }
        });
    }])

    .controller('disputeTrackingCtrl',
    ['$scope', 'modalService', 'baseDataProxy', 'disputeTrackingProxy',
       function ($scope, modalService, baseDataProxy, disputeTrackingProxy) {

           var filterFlg = false;
           $scope.disputeType = "0";
           $scope.totalNum = "";
           $scope.floatMenuOwner = ['disputeTrackingCtrl'];

           $scope.disputeTrackingList = {
               multiSelect: false,
               enableFullRowSelection: true,
               columnDefs: [
                   { field: 'customerName', displayName: 'Customer Name' },
                   { field: 'collector', displayName: 'Collector' },
                   //{ field: 'contact', displayName: 'Customer Contact' },
                   { field: 'billGroupCode', displayName: 'Factory Group Code' },
                   { field: 'billGroupName', displayName: 'Factory Group Name' },
                   { field: 'issueReason', displayName: 'Issue Reason' },
                   { field: 'createDate', displayName: 'Dispute Date', cellFilter: 'date:\'yyyy-MM-dd hh:mm:ss\'', },
                   { field: 'disputeStatus', displayName: 'Status' }
               ],

               onRegisterApi: function (gridApi) {
                   //set gridApi on scope
                   $scope.gridApi = gridApi;

                   //*************************Contact **************************************s
                   gridApi.selection.on.rowSelectionChanged($scope, function (row) {
                       var disputeId = row.entity.id;

                       window.open('#/disputetracking/dispute/' + disputeId);
                   });
                   //*************************Contact **************************************e
               }
           };

           //issue reason status binding
           baseDataProxy.SysTypeDetail("025", function (list) {
               $scope.issueReasonList = list;
           });

           //dispute status binding
           baseDataProxy.SysTypeDetail("026", function (list) {
               list.pop();
               list.pop();
               $scope.statusList = list;
           });

           //invoice status binding
           baseDataProxy.SysTypeDetail("004", function (istatuslist) {
               $scope.istatuslist = istatuslist;
           });
           //Invoice Track States DropDownList binding
           baseDataProxy.SysTypeDetail("029", function (invoiceTrackStatesList) {
               $scope.invoiceTrackStates = invoiceTrackStatesList;
           });

           //*************************aging paging******************************************s
           //paging init
           $scope.selectedLevel = 15;  //init paging size(ng-model)
           $scope.itemsperpage = 15;   //init paging size(parameter)
           $scope.currentPage = 1;     //init page
           $scope.maxSize = 10; //paging display max

           var filstr = "";
           disputeTrackingProxy.disputeTrackingPaging($scope.currentPage, $scope.itemsperpage, "&$filter=(DisputeType eq '" + $scope.disputeType + "')", function (list) {
               $scope.totalItems = list[0].count; //init count
               $scope.disputeTrackingList.data = list[0].results; //init list
               $scope.totalNum = list[0].count;
               $scope.calculate(list[0].results.length);
               $scope.$broadcast("FLOAT_MENU_REFRESH", $scope.floatMenuOwner[0]);
           }, function (error) {
               alert(error);
           });

           //paging size
           $scope.levelList = [
                   { "id": 15, "levelName": '15' },
                   { "id": 500, "levelName": '500' },
                   { "id": 1000, "levelName": '1000' },
                   { "id": 2000, "levelName": '2000' },
                   { "id": 5000, "levelName": '5000' },
                   { "id": 999999, "levelName": 'ALL' }
           ];
           //paging size change
           $scope.pagesizechange = function (selectedLevelId) {

               //multi-conditions
               filstr = buildFilter();

               var index = $scope.currentPage;

               if (filterFlg) {
                   disputeTrackingProxy.disputeTrackingPaging(index, selectedLevelId, filstr, function (list) {
                       $scope.itemsperpage = selectedLevelId;
                       $scope.disputeTrackingList.data = list[0].results;
                       $scope.totalItems = list[0].count;
                       $scope.totalNum = list[0].count;
                       $scope.calculate(list[0].results.length);
                   });
               } else {
                   disputeTrackingProxy.disputeTrackingPaging(index, selectedLevelId, filstr, function (list) {
                       $scope.itemsperpage = selectedLevelId;
                       $scope.disputeTrackingList.data = list[0].results;
                       $scope.totalItems = list[0].count;
                       $scope.totalNum = list[0].count;
                       $scope.calculate(list[0].results.length);
                   });
               }
           };

           //paging change
           $scope.pageChanged = function () {

               //multi-conditions
               filstr = buildFilter();

               var index = $scope.currentPage;

               if (filterFlg) {
                   disputeTrackingProxy.disputeTrackingPaging(index, $scope.itemsperpage, filstr, function (list) {
                       $scope.disputeTrackingList.data = list[0].results;
                       $scope.totalItems = list[0].count;
                       $scope.totalNum = list[0].count;
                       $scope.calculate(list[0].results.length);
                   }, function (error) {
                       alert(error);
                   });
               } else {
                   disputeTrackingProxy.disputeTrackingPaging(index, $scope.itemsperpage, filstr, function (list) {
                       $scope.disputeTrackingList.data = list[0].results;
                       $scope.totalItems = list[0].count;
                       $scope.totalNum = list[0].count;
                       $scope.calculate(list[0].results.length);
                   }, function (error) {
                       alert(error);
                   });
               }
           };
           //*************************aging paging******************************************e

           //openfilter
           var isShow = 0; //0:hide;1:show
           $scope.openFilter = function () {
               if (isShow == 0) {
                   $("#divAgingSearch").show();
                   isShow = 1;
               } else if (isShow == 1) {
                   $("#divAgingSearch").hide();
                   isShow = 0;
               }
           }

           //reset Search conditions
           $scope.resetSearch = function () {
               filstr = "";
               $scope.custName = "";
               $scope.from = "";
               $scope.to = "";
               $scope.issueReason = "";
               $scope.status = "";
               $scope.disputeType = "0";
               $scope.cancelled = "";
               $scope.closed = "";
               //2016-01-07
               $scope.billGroupCode = "";
               $scope.billGroupName = "";
               $scope.states = "";
               $scope.trackStates = "";
               //2016-01-07
           }

           buildFilter = function () {
               if ($scope.cancelled || $scope.closed) {
                   $scope.disputeType = "1";
               } else {
                   $scope.disputeType = "0";
               }

               //multi-conditions
               var filterStr = "&$filter=(DisputeType eq '" + $scope.disputeType + "')";
               if ($scope.custName) {
                   if (filterStr != "") {
                       filterStr += " and (contains(CustomerName,'" + escape($scope.custName.replace("'","''")) + "'))";
                   } else {
                       filterStr += "&$filter=(contains(CustomerName,'" + escape($scope.custName.replace("'", "''")) + "'))";
                   }
               }
               
               //added by zhangYu //2016-01-07
               //invoice states
               if ($scope.states) {
                   filterStr += " and (contains(States,'" + $scope.states + "'))";

               }
               //invoice Track States
               if ($scope.trackStates) {
                   filterStr += " and (contains(TrackStates,'" + $scope.trackStates + "'))";
               }
               //bill Grop Code
               if ($scope.billGroupCode) {
                   filterStr += " and (contains(BillGroupCode, '" + encodeURIComponent($scope.billGroupCode.replace("'", "''")) + "'))";

               }
               //bill Grop Code
               if ($scope.billGroupName) {
                   filterStr += " and (contains(BillGroupName, '" + encodeURIComponent($scope.billGroupName.replace("'", "''")) + "'))";

               }
               //added by zhangYu //2016-01-07

               if ($scope.from) {
                   if (filterStr != "") {
                       filterStr += " and (date(CreateDate) ge " + $scope.from + ")";
                   } else {
                       filterStr += "&$filter=(date(CreateDate) ge " + $scope.from + ")";
                   }
               }
               if ($scope.to) {
                   if (filterStr != "") {
                       filterStr += " and (date(CreateDate) le " + $scope.to + ")";
                   } else {
                       filterStr += "&$filter=(date(CreateDate) le " + $scope.to + ")";
                   }
               }
               if ($scope.issueReason) {
                   if (filterStr != "") {
                       filterStr += " and (IssueReason eq '" + $scope.issueReason + "')";
                   } else {
                       filterStr += "&$filter=(IssueReason eq '" + $scope.issueReason + "')";
                   }
               }
               if ($scope.status) {
                   if (filterStr != "") {
                       filterStr += " and (DisputeStatus eq '" + $scope.status + "')";
                   } else {
                       filterStr += "&$filter=(DisputeStatus eq '" + $scope.status + "')";
                   }
               }
               if ($scope.cancelled && $scope.closed) {
                   if (filterStr != "") {
                       filterStr += " and (DisputeStatus eq 'Cancelled' or DisputeStatus eq 'Closed')";
                   } else {
                       filterStr += "&$filter=(DisputeStatus eq 'Cancelled' or DisputeStatus eq 'Closed')";
                   }
               } else {
                   if ($scope.cancelled) {
                       if (filterStr != "") {
                           filterStr += " and (DisputeStatus eq 'Cancelled')";
                       } else {
                           filterStr += "&$filter=(DisputeStatus eq 'Cancelled')";
                       }
                   }
                   if ($scope.closed) {
                       if (filterStr != "") {
                           filterStr += " and (DisputeStatus eq 'Closed')";
                       } else {
                           filterStr += "&$filter=(DisputeStatus eq 'Closed')";
                       }
                   }
               }

               return filterStr;
           };

           //Do search
           $scope.searchCollection = function () {

               filterFlg = true;

               //multi-conditions
               filstr = buildFilter();
               //current page
               $scope.currentPage = 1;

               disputeTrackingProxy.disputeTrackingPaging($scope.currentPage, $scope.itemsperpage, filstr, function (list) {
                   $scope.totalItems = list[0].count; //init count
                   $scope.disputeTrackingList.data = list[0].results; //init list
                   $scope.totalNum = list[0].count;
                   $scope.calculate(list[0].results.length);
               }, function (error) {
                   alert(error);
               });

           };

           //footer items calculate
           $scope.calculate = function (count) {
               if (count == 0) {
                   $scope.fromItem = 0;
               } else {
                   $scope.fromItem = ($scope.currentPage - 1) * $scope.itemsperpage + 1;
               }
               $scope.toItem = ($scope.currentPage - 1) * $scope.itemsperpage + count;
           }
       }])